      SUBROUTINE TGLOC05A(THISNAME,TSEC50,SCPOS,SCVEL,
     *            TIMTARG,TARG,LUERR,IERR)
      IMPLICIT REAL*8 (A-H,O-Z)
C
C THIS ROUTINE IS PART OF THE TOSS TARGET LOCATION PACKAGE, TGLOC.
C
C THIS IS A SEVICE ROUTINE FOR THE TARGET LOCATION PACKAGE.  IT FINDS
C THE NOON/MIDNIGHT ZENITH DIRECTION VECTOR FOR THE ALGORITHM WHERE
C NOON IS DEFINED AS OCCURRING MIDWAY IN TIME BETWEEN SUNRISE AND
C SUNSET AND MIDNIGHT IS MIDWAY IN TIME BETWEEN SUNSET AND SUNRISE.
C
C VAR     DIM    TYPE I/O DESCRIPTION
C ---     ---    ---- --- -----------
C
C THISNAME 1     CH*16 I  NAME OF THE ZENITH TARGET.  THE ONLY VALID
C                         NAMES ARE 'NOONZENITH1' AND 'MIDZENITH1'.
C
C TSEC50   1      R*8  I  THE TIME ASSOCIATED WITH SCPOS AND SCVEL. TIME
C                         IS NEEDED BECAUSE THE SUN POSITION IS
C                         INVOLVED.  IN SECONDS SINCE 1/1/50, 0.0 HR
C
C SCPOS    3      R*8  I  THE SPACECRAFT POSITION VECTOR AT TIME TSEC50.
C                         THE S/C STATE IS NEEDED BECAUSE ZENITH DEPENDS
C                         ON THE TRAJECTORY.  MEAN OF 1950.0 IS ASSUMED.
C                         IN KM.
C
C SCVEL    3      R*8  I  THE SPACECRAFT VELOCITY VECTOR AT TIME TSEC50.
C                         MEAN OF 1950.0 IS ASSUMED.  IN KM/SEC.
C
C TIMTARG  1      R*8  O  THE TIME WHEN THE S/C ARRIVES AT THE NOON OR
C                         MIDNIGHT POSITION.  IN SECONDS SINCE
C                         1/1/50, 0.0 HRS
C
C TARG     3      R*8  O  TARG IS THE CARTESIAN UNIT VECTOR OF THE
C                         NOON/MIDNIGHT ZENITH TARGET. IN MEAN OF 1950.0
C
C LUERR    1      I*4  I  FORTRAN UNIT NUMBER FOR ERROR MESSAGES.
C                          = 0/NEGATIVE, NO MESSAGES POSSIBLE.
C
C IERR     1      I*4  O  ERROR RETURN FLAG.
C                          = 0, NO ERROR.
C                          = OTHERWISE, ERROR.
C
C***********************************************************************
C
C BY C PETRUZZO, GSFC/742, 8/85.
C        MODIFIED....
C
C***********************************************************************
C
      REAL*8 SUNPOS(3),ELEMS(6),POS(3),VEL(3),SCPOS(3),SCVEL(3),TARG(3)
      REAL*8 HALFPI/ 1.570796326794897D0 /
      REAL*8 PI/ 3.141592653589793D0 /
      REAL*8 TWOPI/ 6.283185307179586D0 /
      REAL*8 DEGRAD/ 57.29577951308232D0 /
      LOGICAL CONVERGED
      CHARACTER*16 THISNAME,NOONZ1/'NOONZENITH1'/,MIDZ1/'MIDZENITH1'/
C
      IBUG = 0
      LUBUG = 19
C
      IF(IBUG.NE.0) WRITE(LUBUG,8701) THISNAME,PAKTIM50(TSEC50),
     *      SCPOS,SCVEL
 8701 FORMAT(/,' TGLOC05A DEBUG. INPUT:  THISNAME= ',A,' TIME=',G20.12/,
     *         '    SCPOS=',3G20.12/,'    SCVEL=',3G20.12)
C
C SET INDICATOR TELLING THE ROUTINE WHICH IS WANTED, NOON OR MIDNIGHT.
C
      KEY = 0
      IF(THISNAME.EQ.NOONZ1) KEY = +1
      IF(THISNAME.EQ.MIDZ1 ) KEY = -1
C
C    ERROR CHECK. IF THIS OCCURS, ROUTINE SHOULD NOT HAVE BEEN CALLED.
      IF(KEY.EQ.0) THEN
        TYPE *,'TGLOC05A. CODING ERROR 1. RUN STOPPED.'
        STOP   'TGLOC05A. CODING ERROR 1. RUN STOPPED.'
        END IF
C
C INITIALIZE MISC PARAMS
C
      IERR = 0
      ERTHMU = CONST(56)
      TIMEPS = 0.1D0  ! FOR CONVERGENCE TEST
C
      CONVERGED = .FALSE.
      MAXLOOPS = 10
      ILOOP = 0
      TIME = TSEC50
      DT = 0.D0
      POS(1) = SCPOS(1)
      POS(2) = SCPOS(2)
      POS(3) = SCPOS(3)
      VEL(1) = SCVEL(1)
      VEL(2) = SCVEL(2)
      VEL(3) = SCVEL(3)
C
C METHOD:  (NOTE THAT NOONMID1 USES THE SUN POSITION AT THE TIME
C OF ITS INPUT STATE VECTOR).  USING TGLOC05A'S INPUT TIME AND STATE,
C CALL NOONMID1 TO DETERMINE THE APPROXIMATE TIME OF NOON OR MIDNIGHT.
C USUALLY, THIS APPROXIMATE TIME WILL BE DIFFERENT FROM THE TIME AT
C WHICH THE SUN POSITION WAS CALCULATED.  SINCE WE WANT THE SUN POSITION
C AT THE NOON OR MIDNIGHT TIME, WE ITERATE BY PROPAGATING THE PREVIOUS
C STATE VECTOR TO THE APPROXIMATE NOON OR MIDNIGHT TIME AND CALL
C NOONMID1 AGAIN.  WE REPEAT THIS UNTIL THE DIFFERENCE IN SUCCESSIVE
C APPROXIMATE TIMES IS WITHIN THE TOLERANCE REQUIRED.
C
      DO WHILE (.NOT.CONVERGED .AND. ILOOP.LT.MAXLOOPS)
        ILOOP = ILOOP + 1
        TIME = TIME + DT
        IF(IBUG.NE.0) WRITE(LUBUG,8901) ILOOP,PAKTIM50(TIME)
 8901   FORMAT(' TGLOC05A DEBUG. START ILOOP=',I3,'  TIME=',G21.13)
        CALL TUBODY(ERTHMU,POS,VEL,DT,POS,VEL,LUERR,IER1)
        IF(IER1.EQ.0) CALL NOONMID1(TIME,POS,VEL,1,TPNOON,TPMIDNT,
     *                          TNNOON,TNMIDNT,1,NOWSTAT,LUERR,IER1)
        IF(IER1.NE.0) THEN
          IERR = 1
          GO TO 9999
          END IF
        IF(KEY.GT.0) THEN
          DTP = TPNOON  - TIME   ! TIME DIFF TO PREVIOUS NOON
          DTN = TNNOON  - TIME   ! TIME DIFF TO NEXT NOON
        ELSE
          DTP = TPMIDNT - TIME   ! TIME DIFF TO PREVIOUS MIDNIGHT
          DTN = TNMIDNT - TIME   ! TIME DIFF TO NEXT MIDNIGHT
          END IF
C      SET DT TO THE SMALLER TIME INTERVAL
        IF(DABS(DTN).LT.DABS(DTP)) THEN
          DT = DTN
        ELSE
          DT = DTP
          END IF
C      CONVERGENCE TEST
        CONVERGED = DABS(DT) .LT. TIMEPS
        IF(IBUG.NE.0) WRITE(LUBUG,8902) ILOOP,CONVERGED,DT,
     *         PAKTIM50(TPNOON),PAKTIM50(TNNOON),
     *         PAKTIM50(TPMIDNT),PAKTIM50(TNMIDNT)
8902    FORMAT(' TGLOC05A DEBUG. END LOOP=',I3,'  CONVERGED=',L2,
     *              '  DT=',G13.5/,
     *           '   TPNOON,TNNOON=   ',2G21.13/,
     *           '   TPMIDNT,TNMIDNT= ',2G21.13)
        END DO
C
C ***********
C * WRAP UP *
C ***********
C
C
      IF(.NOT.CONVERGED) THEN
C      MAX ITERATIONS HAVE BEEN DONE. ERROR.
        IERR = 1
        IF(LUERR.GT.0) WRITE(LUERR,9001)
 9001   FORMAT(/,
     *   ' TGLOC05A. MAX ITERATIONS EXCEEDED. BAD NOON OR MIDNIGHT ',
     *       ' ZENITH LOCATION COMPUTED.')
        GO TO 9999
      ELSE
        TIMTARG = TIME
        CALL VNORM(POS,TARG)
        END IF
C
      IF(IBUG.NE.0) WRITE(LUBUG,8765) PAKTIM50(TIMTARG),TARG
 8765 FORMAT(/,' TGLOC05A DEBUG.  RETURN INFO:  TIME=',G20.12/,
     *         '   UNIT VECTOR=',3G16.8)
C
 9999 CONTINUE
      RETURN
      END
